package com.tiger.easyrpc.core.extension;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static com.tiger.easyrpc.common.EasyrpcConstant.COMMON_SYMBOL_DYH;

public class ExtensionWrapper {
    private static Logger logger = LoggerFactory.getLogger(ExtensionWrapper.class);
    private static String[] scanPath = {
            "META-INF/easyrpc/"
    };
    private Class<?> defaultExtension;
    private Map<String,Class<?>> extensionWrappers = new ConcurrentHashMap();

    public void addExtension(Object o){

    }

    public void setDefaultExtension(Class defaultExtension){
        this.defaultExtension = defaultExtension;
    }

    public Class getDefaultExtension(){
        return this.defaultExtension;
    }

    public void setExtension(String type,Class t){
        extensionWrappers.put(type,t);
    }

    public Class getExtension(String className){
        Class t = extensionWrappers.get(className);
        if(t == null){
            loadClass(className);
        }
        return extensionWrappers.get(className);
    }


    private void loadClass(String className){
        ClassLoader classLoader = ExtensionLoader.class.getClassLoader();
        for (String s : scanPath) {
            s = s + className;
            URL resource = classLoader.getResource(s);
            BufferedReader bufferedReader = null;
            InputStreamReader inputStreamReader = null;
            InputStream inputStream = null;
            try {
                inputStream = resource.openStream();
                inputStreamReader =  new InputStreamReader(inputStream);
                bufferedReader = new BufferedReader(inputStreamReader);
                String info = null;
                while((info = bufferedReader.readLine()) !=null){
                    String[] split = info.split(COMMON_SYMBOL_DYH);
                    if(split.length != 2){
                        throw new IllegalArgumentException("the format is incorrect! key = value");
                    }
                    String type = split[0].trim();
                    String clazStr = split[1].trim();
                    try {
                        Class claz = classLoader.loadClass(clazStr);
                        extensionWrappers.put(type,claz);
                    } catch (ClassNotFoundException e) {
                        logger.error("the Class {} not founc, please check it",clazStr);
                    }
                }

            } catch (IOException e) {
                e.printStackTrace();
            }  finally {
                try {
                    if(bufferedReader != null){
                        bufferedReader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
